﻿using System;
using System.Windows.Forms;
using CharacterTrailClient.Message;
using CharacterTrailLibrary.Factory;
using CharacterTrailLibrary.Interface;
using CharacterTrailLibrary.Sort;

namespace CharacterTrailClient.Windows
{
    public partial class FormSequenceTracking : FormBase
    {
        private readonly IBarCodeTracking _barCodeTrackingImpl = InstanceFactory.GetBarCodeTracking();

        public FormSequenceTracking()
        {
            InitializeComponent();
            gridView2.Columns.ColumnByFieldName("RepairNote").ColumnEdit = base.RepMemoEdit;
        }

        public override string HeaderText
        {
            get { return "  序列号跟踪  "; }
        }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            try
            {
                var barCode = _barCodeTrackingImpl.GetTopProductByBarCode(textEditBarCode.Text.Trim());
                if (!String.IsNullOrEmpty(barCode))
                {
                    var list = _barCodeTrackingImpl.GetVProductDetailHeaderBaseModels(barCode);
                    var repairList = _barCodeTrackingImpl.GetRelateVRepairBaseModels(list);

                    list.Sort(new VProductDetailHeaderBaseModelComparer());
                    gridControl1.DataSource = list;

                    repairList.Sort(new VRepairBaseModelComparer());
                    gridControl2.DataSource = repairList;
                }
                else
                {
                    MsgBox.ShowWarning("没有找到该序列号的任何信息！");
                }
            }
            catch (Exception exception)
            {
                MsgBox.ShowError(exception.Message);
            }
        }

        private void TextEditBarCodeKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char) Keys.Enter)
            {
                CmdQuery_Click(null, null);
            }
        }
    }
}